<?php
/* 
 DOWNLOAD.INC.PHP by martin $ 2008/07/27 18:08:53
*/

checkADMIN();

$dl_file = OD.'dl_count'.EXT; # log

$LINES = is_file($dl_file) ? file($dl_file) : array();

$target = '';

if(g_('aim') != ''){
 $aim = basename(g_('aim'));
 $d = opendir(AFFIXED_DIR);
 while ($file = readdir($d)){
  if(ereg("^$aim$", $file)) $target = AFFIXED_DIR.$file;
 }
 closedir($d);
 if(ADMIN==TRUE){
  $d = opendir(MEDIA_DIR);
  while ($file = readdir($d)){
   if(ereg("^$aim$", $file)) $target = MEDIA_DIR.$file;
  }
  closedir($d);
  if($target=='' && !strstr($aim, '.txt')){
   $d = opendir(LOG);
   while ($file = readdir($d)){
    if(ereg("^$aim$", $file)) $target = LOG.$file;
   }
   closedir($d);
  }
  if($target=='' && preg_match('{\d+?_\d+?\.txt}', $aim)){
   $d = opendir('stat/data/');
   while ($file = readdir($d)){
    if(ereg("^$aim$", $file)) $target = LOG.$file;
   }
   closedir($d);
  }
 }
}

$exists = FALSE;

if($target!=''){
 if(!empty($LINES)){
  foreach($LINES as $index=>$line){
   list($dltime,$filename,$dlcount) = explode('|', $line);
   if(ereg("$filename", $target)){
    $dlcount = $dlcount + 1;
    $data = time().'|'.$filename.'|'.$dlcount.NL;
    array_splice($LINES, $index, 1, $data);
    $exists = TRUE;
    break;
   }
  }
 }
 if(!$exists) $LINES[] = time().'|'.$target.'|0'.NL;
} else return $DIVISION['body'] .= '<h3 class="alert center">ファイル：'.g_('aim').' が見当たりません。</h3>'.NL;

rewrite($dl_file, $LINES);
header("Content-Type: application/octet-stream; name=\"".basename($target)."\"");
header("Content-Length: ".filesize($target));
header("Content-Disposition: attachment; filename=\"".basename($target)."\"");
@readfile($target);
exit;

return $DIVISION['body'] .= '<h3 class="alert center">ファイル：'.g_('aim').' が見当たりません</h3>'.NL;
?>
